#include "settingitemdelegate.h"
#include <QPainter>

SettingItemDelegate::SettingItemDelegate(QWidget *parent):QStyledItemDelegate(parent)
{

}

void SettingItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                const QModelIndex &index) const{
    Q_UNUSED(index);
    painter->save();

    //绘制背景
    if(option.state & QStyle::State_Selected ){
        painter->fillRect(option.rect,QColor(230,230,230));
    }else if(option.state & QStyle::State_MouseOver){
        painter->fillRect(option.rect,QColor(245,245,245));
    }else{
        painter->fillRect(option.rect,Qt::white);
    }

    //获取自定义widget
    QWidget* widget = qobject_cast<QWidget*>(option.styleObject);
    if(!widget){
        painter->restore();
        return;
    }

    //获取自定义内容
    if(QWidget* itemWidget = qobject_cast<QWidget*>(widget->property("itemWidget").value<QObject*>())){
        itemWidget->render(painter,option.rect.topLeft(),QRegion(),QWidget::DrawChildren);
    }

    painter->restore();
}

QSize SettingItemDelegate::sizeHint(const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(200,85);
}
